/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import java.util.*;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.*;
import java.text.NumberFormat;
import java.io.File;
import java.util.List;
import java.util.Iterator;
import net.coobird.thumbnailator.*;

import Models.MonAn_Model;
import Models.NhatKyHT_Model;

import Models.NhomMA_Model;
import Models.NhanVien_Model;
import Models.TuyChon_Model;
import Beans.NhanVien;

/**
 *
 * @author Venus
 */
public class MonAn_Controller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            if (session.getAttribute("langid") == null) {
                response.sendRedirect("index.jsp");
                return;
            }
            
            String action = request.getParameter("action");
            int langid = (Integer) session.getAttribute("langid");
            NumberFormat nf = NumberFormat.getNumberInstance(Locale.FRENCH);
            if (action.equals("Add")) {
                String maquyen = "25";
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                if (NhanVien_Model.CheckQuyen(Integer.parseInt(maquyen), nv.getMaNV())) {
                    String MaNMA = "";
                    String TenMA = "";
                    String TenMAVT = "";
                    String MoTa = "";
                    String rimage = "";
                    String dvt = "";
                    String price = "";
                    int MaMA = MonAn_Model.TaoMa();
                    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
                    if (isMultipart) {
                        FileItemFactory factory = new DiskFileItemFactory();
                        ServletFileUpload upload = new ServletFileUpload(factory);
                        upload.setHeaderEncoding("UTF-8");
                        List items = null;
                        try {
                            items = upload.parseRequest(request);
                        } catch (FileUploadException e) {
                            e.printStackTrace();
                        }
                        Iterator itr = items.iterator();

                        while (itr.hasNext()) {
                            FileItem item = (FileItem) itr.next();
                            if (item.isFormField()) {
                                // đối với các text box
                                String name = item.getFieldName();// thuộc tính name của thẻ input
                                String tam = item.getString();
                                byte[] bytes = tam.getBytes("ISO-8859-1");
                                String value = new String(bytes, "UTF-8");// giá trị được nhập trên input
                                if (value == null) {
                                    value = "";
                                }
                                if (name.equals("nhommonan")) {
                                    MaNMA = value;
                                }
                                if (name.equals("tenmonan")) {
                                    TenMA = value;
                                }
                                if (name.equals("tenviettat")) {
                                    TenMAVT = value;
                                }
                                if (name.equals("mota")) {
                                    MoTa = value;
                                }
                                if (name.equals("donvitinh")) {
                                    dvt = dvt + value + ";";
                                }
                                if (name.equals("gia")) {
                                    if (value.equals("")) {
                                        price = price + "0" + ";";
                                    } else {
                                        price = price + value + ";";
                                    }
                                }
                            } else {
                                // đối với file upload
                                try {
                                    String tenfile = item.getName();
                                    String[] sp = tenfile.split("\\.");
                                    rimage = MaMA + "." + sp[sp.length - 1];
                                    File file1 = new File(getServletContext().getRealPath("/") + "images/foodimages/" + rimage.toLowerCase());
                                    String realpath = getServletContext().getRealPath("/");
                                    String path = realpath.substring(0, realpath.indexOf("build\\web\\"));
                                    //File file2 = new File(path+"web/images/foodimages/"+rimage);

                                    item.write(file1);
                                    //file2.renameTo(new File(file2, file1.getName()));
                                    //item.write(file1);
                                    String path1 = getServletContext().getRealPath("/") + "images/foodimages/";
                                    out.print(getServletContext().getRealPath("/"));
                                    int length = getServletContext().getRealPath("/").length();
                                    String web = getServletContext().getRealPath("/").substring(0, length - 10) + "web/";
                                    Thumbnails.of(new File(path1 + rimage.toLowerCase())).height(70).toFile(new File(getServletContext().getRealPath("/") + "images/foodimages/mobile/" + rimage.toLowerCase()));
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }
                        }
                    }
                    String donvitinh[] = dvt.split(";");
                    String gia[] = price.split(";");
                    int j = 0;
                    int k = 0;
                    while (j < donvitinh.length) {
                        if (!donvitinh[j].equals("")) {
                            int MaDVT = MonAn_Model.LayMaDVT(donvitinh[j], langid);
                            String MaTTe = MonAn_Model.LayMaTTe(langid);
                            if (MonAn_Model.CheckDVTMA(MaDVT, MaMA) == false) {
                                MonAn_Model.AddDVTMA(MaMA, MaDVT);
                                if (MonAn_Model.AddGia(MaMA, MaDVT, MaTTe, gia[j])) {
                                    k = k + 1;
                                }
                                MonAn_Model.AddLichSuGia(MaMA, MaDVT, gia[j]);
                            }
                        }
                        j = j + 1;
                    }
                    MonAn_Model.AddMonAn(MaMA, TenMA, TenMAVT, MaNMA, MoTa);
                    MonAn_Model.AddHinhAnhMonAn(MaMA, rimage.toLowerCase());
                    NhomMA_Model nmam = new NhomMA_Model();
                    Vector vDSNMA = new Vector();
                    vDSNMA = nmam.LayDSNMAA(langid);
                    String item = "1";
                    Vector vMA = new Vector();
                    vMA = MonAn_Model.LayDSMA(langid);
                    nv = (NhanVien) session.getAttribute("nv");
                    NhatKyHT_Model.GhiNhatKy(2, nv.getMaNV(), "food");
                    if (k != 0) {
                        NhatKyHT_Model.GhiNhatKy(2, nv.getMaNV(), "foodprice");
                    }
                    session.setAttribute("item", item);
                    session.setAttribute("DSNMA", vDSNMA);
                    session.setAttribute("DSMA", vMA);
                    response.sendRedirect("admin/listfood.jsp");
                } else {
                    response.sendRedirect("admin/admin.jsp");
                }
            } else if (action.equals("Show")) { 
                String maquyen = "24";
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                if (NhanVien_Model.CheckQuyen(Integer.parseInt(maquyen), nv.getMaNV())) {
                    NhomMA_Model nmam = new NhomMA_Model();
                    Vector vDSNMA = new Vector();
                    vDSNMA = nmam.LayDSNMA(langid);
                    String item = request.getParameter("item");
                    Vector vMA = new Vector();
                    vMA = MonAn_Model.LayDSMA(langid);

//                    request.setAttribute("DSMA", vMA);
//                    request.setAttribute("DSNMA", vDSNMA);
//                    request.setAttribute("item", item);
//                    RequestDispatcher rd = request.getRequestDispatcher("admin/food/listfood.jsp");
//                    rd.forward(request, response);

                    session.setAttribute("DSMA", vMA);
                    session.setAttribute("DSNMA", vDSNMA);
                    session.setAttribute("item", item);
                    response.sendRedirect("admin/listfood.jsp");
                } else {
                    response.sendRedirect("admin/admin.jsp");
                }
            } else if (action.equals("AddFood")) {
                NhomMA_Model nmam = new NhomMA_Model();
                Vector vDSNMA = new Vector();
                vDSNMA = nmam.LayDSNMAA(langid);
                request.setAttribute("DSNMA", vDSNMA);
                RequestDispatcher rd = request.getRequestDispatcher("admin/newfood.jsp");
                rd.forward(request, response);
            } else if (action.equals("OptionFood")) {
                String mama = request.getParameter("mama");
                MonAn_Model mam = new MonAn_Model();
                Vector vMA = new Vector();
                vMA = mam.Detail(mama, langid);
                Vector vTCMA = new Vector();
                vTCMA = MonAn_Model.LayTuyChonMA(mama);
                request.setAttribute("MA", vMA);
                request.setAttribute("TCMA", vTCMA);
                request.setAttribute("mama", mama);
                RequestDispatcher rd = request.getRequestDispatcher("admin/optionfood.jsp");
                rd.forward(request, response);
            } else if (action.equals("UpdateStatusMA")) {
                String mama = request.getParameter("mama");
                String status = request.getParameter("status");
                String item = request.getParameter("item");
                MonAn_Model.UpdateStatusMA(mama, status);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "statusfood");
                NhomMA_Model nmam = new NhomMA_Model();
                Vector vDSNMA = new Vector();
                vDSNMA = nmam.LayDSNMAA(langid);
                Vector vMA = new Vector();
                vMA = MonAn_Model.LayDSMA(langid);
                session.setAttribute("DSMA", vMA);
                session.setAttribute("DSNMA", vDSNMA);
                session.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rslistfood.jsp");
                rd.forward(request, response);
            } else if (action.equals("SearchMA")) {
                String nhommonan = request.getParameter("nhommonan");
                String TENMA = request.getParameter("TENMA");
                String TENVT = request.getParameter("TENVT");
                String GIA = request.getParameter("GIA");
                String status = request.getParameter("status");
                String item = request.getParameter("item");
                MonAn_Model mam = new MonAn_Model();
                Vector v = new Vector();
                v = mam.SearchMA(nhommonan, TENMA, TENVT, GIA, status, langid);
                NhomMA_Model nmam = new NhomMA_Model();
                Vector vDSNMA = new Vector();
                vDSNMA = nmam.LayDSNMATK(nhommonan, langid);
                request.setAttribute("status", status);
                request.setAttribute("nhommonan", nhommonan);
                request.setAttribute("TENMA", TENMA);
                request.setAttribute("TENVT", TENVT);
                request.setAttribute("GIA", GIA);
                request.setAttribute("item", item);
                request.setAttribute("DSMA", v);
                request.setAttribute("DSNMA", vDSNMA);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssearchfood.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortTenNMA")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                MonAn_Model mam = new MonAn_Model();
                Vector vDSMA = new Vector();
                vDSMA = mam.SortTenNMA(langid, order);
                NhomMA_Model nmam = new NhomMA_Model();
                Vector vDSNMA = new Vector();
                vDSNMA = nmam.LayDSNMAA(langid);
                request.setAttribute("DSMA", vDSMA);
                request.setAttribute("DSNMA", vDSNMA);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortfood.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortTenMA")) {   // Sort tên khu vực
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                MonAn_Model mam = new MonAn_Model();
                Vector vDSMA = new Vector();
                vDSMA = mam.SortTenMA(langid, order);
                NhomMA_Model nmam = new NhomMA_Model();
                Vector vDSNMA = new Vector();
                vDSNMA = nmam.LayDSNMAA(langid);
                request.setAttribute("DSMA", vDSMA);
                request.setAttribute("DSNMA", vDSNMA);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortfood.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortTenVT")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                MonAn_Model mam = new MonAn_Model();
                Vector vDSMA = new Vector();
                vDSMA = mam.SortTenVT(langid, order);
                NhomMA_Model nmam = new NhomMA_Model();
                Vector vDSNMA = new Vector();
                vDSNMA = nmam.LayDSNMAA(langid);
                request.setAttribute("DSMA", vDSMA);
                request.setAttribute("DSNMA", vDSNMA);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortfood.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortGiaMA")) {   // Sort tên khu vực
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                MonAn_Model mam = new MonAn_Model();
                Vector vDSMA = new Vector();
                vDSMA = mam.SortGiaMA(langid, order);
                NhomMA_Model nmam = new NhomMA_Model();
                Vector vDSNMA = new Vector();
                vDSNMA = nmam.LayDSNMAA(langid);
                request.setAttribute("DSMA", vDSMA);
                request.setAttribute("DSNMA", vDSNMA);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "5");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortfood.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortTTMA")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                MonAn_Model mam = new MonAn_Model();
                Vector vDSMA = new Vector();
                vDSMA = mam.SortTTMA(langid, order);
                NhomMA_Model nmam = new NhomMA_Model();
                Vector vDSNMA = new Vector();
                vDSNMA = nmam.LayDSNMAA(langid);
                request.setAttribute("DSMA", vDSMA);
                request.setAttribute("DSNMA", vDSNMA);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "6");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortfood.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchTenNMA")) {
                String nhommonan = request.getParameter("nhommonan");
                String TENMA = request.getParameter("TENMA");
                String TENVT = request.getParameter("TENVT");
                String GIA = request.getParameter("GIA");
                String status = request.getParameter("status");
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                MonAn_Model mam = new MonAn_Model();
                Vector vDSMA = new Vector();
                vDSMA = mam.SortSearchTenNMA(nhommonan, TENMA, TENVT, GIA, status, order, langid);
                NhomMA_Model nmam = new NhomMA_Model();
                Vector vDSNMA = new Vector();
                vDSNMA = nmam.LayDSNMATK(nhommonan, langid);
                request.setAttribute("status", status);
                request.setAttribute("nhommonan", nhommonan);
                request.setAttribute("TENMA", TENMA);
                request.setAttribute("TENVT", TENVT);
                request.setAttribute("GIA", GIA);
                request.setAttribute("DSMA", vDSMA);
                request.setAttribute("DSNMA", vDSNMA);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchfood.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchTenMA")) {
                String nhommonan = request.getParameter("nhommonan");
                String TENMA = request.getParameter("TENMA");
                String TENVT = request.getParameter("TENVT");
                String GIA = request.getParameter("GIA");
                String status = request.getParameter("status");
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                MonAn_Model mam = new MonAn_Model();
                Vector vDSMA = new Vector();
                vDSMA = mam.SortSearchTenMA(nhommonan, TENMA, TENVT, GIA, status, order, langid);
                NhomMA_Model nmam = new NhomMA_Model();
                Vector vDSNMA = new Vector();
                vDSNMA = nmam.LayDSNMATK(nhommonan, langid);
                request.setAttribute("status", status);
                request.setAttribute("nhommonan", nhommonan);
                request.setAttribute("TENMA", TENMA);
                request.setAttribute("TENVT", TENVT);
                request.setAttribute("GIA", GIA);
                request.setAttribute("DSMA", vDSMA);
                request.setAttribute("DSNMA", vDSNMA);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchfood.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchTenVT")) {
                String nhommonan = request.getParameter("nhommonan");
                String TENMA = request.getParameter("TENMA");
                String TENVT = request.getParameter("TENVT");
                String GIA = request.getParameter("GIA");
                String status = request.getParameter("status");
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                MonAn_Model mam = new MonAn_Model();
                Vector vDSMA = new Vector();
                vDSMA = mam.SortSearchTenVT(nhommonan, TENMA, TENVT, GIA, status, order, langid);
                NhomMA_Model nmam = new NhomMA_Model();
                Vector vDSNMA = new Vector();
                vDSNMA = nmam.LayDSNMATK(nhommonan, langid);
                request.setAttribute("status", status);
                request.setAttribute("nhommonan", nhommonan);
                request.setAttribute("TENMA", TENMA);
                request.setAttribute("TENVT", TENVT);
                request.setAttribute("GIA", GIA);
                request.setAttribute("DSMA", vDSMA);
                request.setAttribute("DSNMA", vDSNMA);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchfood.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchGiaMA")) {
                String nhommonan = request.getParameter("nhommonan");
                String TENMA = request.getParameter("TENMA");
                String TENVT = request.getParameter("TENVT");
                String GIA = request.getParameter("GIA");
                String status = request.getParameter("status");
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                MonAn_Model mam = new MonAn_Model();
                Vector vDSMA = new Vector();
                vDSMA = mam.SortSearchGiaMA(nhommonan, TENMA, TENVT, GIA, status, order, langid);
                NhomMA_Model nmam = new NhomMA_Model();
                Vector vDSNMA = new Vector();
                vDSNMA = nmam.LayDSNMATK(nhommonan, langid);
                request.setAttribute("status", status);
                request.setAttribute("nhommonan", nhommonan);
                request.setAttribute("TENMA", TENMA);
                request.setAttribute("TENVT", TENVT);
                request.setAttribute("GIA", GIA);
                request.setAttribute("DSMA", vDSMA);
                request.setAttribute("DSNMA", vDSNMA);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "5");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchfood.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchTTMA")) {
                String nhommonan = request.getParameter("nhommonan");
                String TENMA = request.getParameter("TENMA");
                String TENVT = request.getParameter("TENVT");
                String GIA = request.getParameter("GIA");
                String status = request.getParameter("status");
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                MonAn_Model mam = new MonAn_Model();
                Vector vDSMA = new Vector();
                vDSMA = mam.SortSearchTTMA(nhommonan, TENMA, TENVT, GIA, status, order, langid);
                NhomMA_Model nmam = new NhomMA_Model();
                Vector vDSNMA = new Vector();
                vDSNMA = nmam.LayDSNMATK(nhommonan, langid);
                request.setAttribute("status", status);
                request.setAttribute("nhommonan", nhommonan);
                request.setAttribute("TENMA", TENMA);
                request.setAttribute("TENVT", TENVT);
                request.setAttribute("GIA", GIA);
                request.setAttribute("DSMA", vDSMA);
                request.setAttribute("DSNMA", vDSNMA);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "6");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchfood.jsp");
                rd.forward(request, response);
            } else if (action.equals("CheckDVT")) {
                String tendvt = request.getParameter("tendvt");
                if (MonAn_Model.CheckDVT(tendvt, langid)) {
                    out.print("exist");
                } else {
                    out.print("error");
                }
            } else if (action.equals("CheckTienTe")) {
                if (MonAn_Model.CheckTienTe(langid)) {
                    out.print("success");
                } else {
                    out.print("error");
                }
            } else if (action.equals("ShowLangMA")) {
                String mama = request.getParameter("mama");
                MonAn_Model mam = new MonAn_Model();
                Vector v = new Vector();
                v = mam.LangMA(Integer.parseInt(mama));
                request.setAttribute("LangMA", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/foodlang.jsp");
                rd.forward(request, response);
            } else if (action.equals("Detail")) {
                String mama = request.getParameter("mama");
                MonAn_Model mam = new MonAn_Model();
                Vector vdetail = new Vector();
                vdetail = mam.Detail(mama, langid);
                Vector vgia = new Vector();
                vgia = mam.LayGia(mama, langid);
                request.setAttribute("detail", vdetail);
                request.setAttribute("vgia", vgia);
                request.setAttribute("mama", mama);
                NhomMA_Model nmam = new NhomMA_Model();
                Vector vDSNMA = new Vector();
                vDSNMA = nmam.LayDSNMAA(langid);
                request.setAttribute("DSNMA", vDSNMA);
                RequestDispatcher rd = request.getRequestDispatcher("admin/detailfood.jsp");
                rd.forward(request, response);
            } else if (action.equals("Edit")) {
                String maquyen = "26";
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                if (NhanVien_Model.CheckQuyen(Integer.parseInt(maquyen), nv.getMaNV())) {
                    String MaNMA = "";
                    String TenMA = "";
                    String TenMAVT = "";
                    String MoTa = "";
                    String rimage = "";
                    String dvt = "";
                    String price = "";
                    String mama = request.getParameter("mama");
                    int n = 0;
                    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
                    if (isMultipart) {
                        FileItemFactory factory = new DiskFileItemFactory();
                        ServletFileUpload upload = new ServletFileUpload(factory);
                        upload.setHeaderEncoding("UTF-8");
                        List items = null;
                        try {
                            items = upload.parseRequest(request);
                        } catch (FileUploadException e) {
                            e.printStackTrace();
                        }
                        Iterator itr = items.iterator();

                        while (itr.hasNext()) {
                            FileItem item = (FileItem) itr.next();
                            if (item.isFormField()) {
                                // đối với các text box
                                String name = item.getFieldName();// thuộc tính name của thẻ input
                                String tam = item.getString();
                                byte[] bytes = tam.getBytes("ISO-8859-1");
                                String value = new String(bytes, "UTF-8");// giá trị được nhập trên input
                                if (value == null) {
                                    value = "";
                                }
                                if (name.equals("nhommonan")) {
                                    MaNMA = value;
                                }
                                if (name.equals("tenmonan")) {
                                    TenMA = value;
                                }
                                if (name.equals("tenviettat")) {
                                    TenMAVT = value;
                                }
                                if (name.equals("mota")) {
                                    MoTa = value;
                                }
                                if (name.equals("donvitinh")) {
                                    dvt = dvt + value + ";";
                                }
                                if (name.equals("gia")) {
                                    if (value.equals("")) {
                                        price = price + "0" + ";";
                                    } else {
                                        price = price + value + ";";
                                    }
                                }
                            } else {
                                // đối với file upload
                                try {
                                    String tenfile = item.getName();
                                    if (!tenfile.equals("")) {
                                        n = n + 1;
                                        String tenhinhanh = MonAn_Model.LayTenHinhAnh(Integer.parseInt(mama));
                                        File delfile = new File(getServletContext().getRealPath("/") + "images/foodimages/" + tenhinhanh.toLowerCase());
                                        delfile.delete();
                                        String[] sp = tenfile.split("\\.");
                                        rimage = mama + "." + sp[sp.length - 1];
                                        File file1 = new File(getServletContext().getRealPath("/") + "images/foodimages/" + rimage.toLowerCase());
                                        String realpath = getServletContext().getRealPath("/");
                                        String path = realpath.substring(0, realpath.indexOf("build\\web\\"));
                                        item.write(file1);

                                        String path1 = getServletContext().getRealPath("/") + "images/foodimages/";
                                        out.print(getServletContext().getRealPath("/"));
                                        int length = getServletContext().getRealPath("/").length();
                                        String web = getServletContext().getRealPath("/").substring(0, length - 10) + "web/";
                                        Thumbnails.of(new File(path1 + rimage.toLowerCase())).height(70).toFile(new File(getServletContext().getRealPath("/") + "images/foodimages/mobile/" + rimage.toLowerCase()));

                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }
                        }
                    }
                    String donvitinh[] = dvt.split(";");
                    String gia[] = price.split(";");
                    int j = 0;
                    MonAn_Model.DeleteDVTMA(Integer.parseInt(mama));
                    MonAn_Model.DeleteGia(Integer.parseInt(mama));
                    while (j < donvitinh.length) {
                        if (!donvitinh[j].equals("")) {
                            int MaDVT = MonAn_Model.LayMaDVT(donvitinh[j], langid);
                            String MaTTe = MonAn_Model.LayMaTTe(langid);
                            if (MonAn_Model.CheckDVTMA(MaDVT, Integer.parseInt(mama)) == false) {
                                MonAn_Model.AddDVTMA(Integer.parseInt(mama), MaDVT);
                                MonAn_Model.AddGia(Integer.parseInt(mama), MaDVT, MaTTe, gia[j]);
                                if (MonAn_Model.CheckLichSuGia(Integer.parseInt(mama), MaDVT, gia[j]) == false) {
                                    MonAn_Model.AddLichSuGia(Integer.parseInt(mama), MaDVT, gia[j]);
                                }
                            }
                        }
                        j = j + 1;
                    }

                    MonAn_Model.UpdateMA(Integer.parseInt(mama), TenMA, TenMAVT, MaNMA, MoTa, langid);
                    if (n != 0) {
                        MonAn_Model.UpdateHinhAnhMonAn(Integer.parseInt(mama), rimage.toLowerCase());
                    }
                    nv = (NhanVien) session.getAttribute("nv");
                    NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "food");
                    NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "foodprice");
                    NhomMA_Model nmam = new NhomMA_Model();
                    Vector vDSNMA = new Vector();
                    vDSNMA = nmam.LayDSNMAA(langid);
                    String item = "1";
                    Vector vMA = new Vector();
                    vMA = MonAn_Model.LayDSMA(langid);
                    session.setAttribute("item", item);
                    session.setAttribute("DSNMA", vDSNMA);
                    session.setAttribute("DSMA", vMA);
                    response.sendRedirect("admin/listfood.jsp");
                } else {
                    response.sendRedirect("admin/admin.jsp");
                }
            } else if (action.equals("EditTenMA")) {
                String tenma = request.getParameter("tenma");
                String mama = request.getParameter("mama");
                String mann = request.getParameter("mann");
                MonAn_Model.EditTenMA(mama, mann, tenma);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "foodname");
                MonAn_Model mam = new MonAn_Model();
                Vector v = new Vector();
                v = mam.LangMA(Integer.parseInt(mama));
                request.setAttribute("LangMA", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/foodlang.jsp");
                rd.forward(request, response);
            } else if (action.equals("AddOptionFood")) {
                String tentc = request.getParameter("tentc");
                String mama = request.getParameter("mama");
                int MaTC = TuyChon_Model.LayMaTC(tentc, langid);
                MonAn_Model.ThemTuyChonMA(MaTC, mama);
                Vector v = new Vector();
                v = MonAn_Model.LayTuyChonMA(mama);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(2, nv.getMaNV(), "optionfood");
                request.setAttribute("TCMA", v);
                request.setAttribute("mama", mama);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rsoptionfood.jsp");
                rd.forward(request, response);
            } else if (action.equals("DeleteOptionFood")) {
                String matc = request.getParameter("matc");
                String mama = request.getParameter("mama");
                MonAn_Model.XoaTuyChonMA(matc, mama);
                Vector v = new Vector();
                v = MonAn_Model.LayTuyChonMA(mama);
                request.setAttribute("TCMA", v);
                request.setAttribute("mama", mama);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rsoptionfood.jsp");
                rd.forward(request, response);
            }

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
